package com.java8.function;

import com.entity.User;
import org.junit.Test;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @Description: 函数编程测试
 * @Author: whp
 * @Date: 2021-02-08 16:14
 */
public class FunctionDemo {

    @Test
    public void testFunction() {

        User user = new User();
        user.setUserName("whp");
        user.setEmail("123@qq.com");

        String name = userGetName.apply(user);
        System.out.println(name);
    }

    @Test
    public void testSupplier() {
        User user = getUser();
        User user1 = getUser();
        Supplier<User> user2 = () -> {
            return new User();
        };


        System.out.println(user == user1);
        System.out.println(user.getEmail());
    }

    /*
     * @Description: TODO
     * @Param: 输入一个参数无返回
     * @return void
     **/
    @Test
    public void testOneParm() {
        Consumer<String> consumer = (s) -> {
            System.out.println(s);
        };
        consumer.accept("2314");
    }

    @Test
    public void testCustomizeFunction() {
        User user = new User();
        user.setEmail("qq.com");

       String e= customizeFunction(user, u -> {
            u.setEmail(u.getEmail().replace(".", "@"));
            return u.getEmail();
        });

        System.out.println(e);
    }

    //-------- method -------------------------------------------------------------

    public static String getName(User user) {
        return userGetName.apply(user);
    }


    public static User getUser() {
        return getUser.get();
    }

    public static String customizeFunction(User user, FunctionDemo1<User> action) {
        String emiail = action.tranEmail(user);
        return emiail;
    }

    //---------- function ------------------------------------------------------------
    /*
     * @Description: 接受一个参数，并返回一个参数
     * @Param:
     * @return
     **/
    private static Function<User, String> userGetName = user -> user.getUserName();

    private static Supplier<User> getUser = () -> {
        User user = new User();
        user.setEmail("12.com");
        return user;
    };

    //输入一个参数

}
